package com.rogers.monster.service.impl;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import com.rogers.monster.dao.ProjectSearchDao;
import com.rogers.monster.service.ProjectService;
import com.rogers.monster.util.M_Util;
import com.rogers.monster.vo.CAProfile;
import com.rogers.monster.vo.Project;
import com.rogers.monster.vo.Reporting_person;
import com.rogers.monster.web.project.ProjectSearchCriteria;

/**
 * @author paul.ning
 * 
 */
public class ProjectServiceImpl implements ProjectService {

	private ProjectSearchDao projectSearchDao;

	public ProjectSearchDao getProjectSearchDao() {
		return projectSearchDao;
	}

	public void setProjectSearchDao(ProjectSearchDao projectSearchDao) {
		this.projectSearchDao = projectSearchDao;
	}

	@SuppressWarnings("unchecked")
	public List<Project> getListOfProjects(ProjectSearchCriteria psc) {
		List<Project> vps = new ArrayList<Project>();
		
		for (Map e : (List<Map>) (projectSearchDao.getListOfProjects(psc))) {
			Project p = new Project();
			p.setProject_id(e.get("PROJECT_ID").toString());
			// p.setProject_number(e.get("	CATEGORY_ID").toString());
			if (!M_Util.isEmpty(e.get("PROJECT_NUMBER"))) {
				p.setProject_number(e.get("PROJECT_NUMBER").toString());
			}

			if (!M_Util.isEmpty(e.get("PROJECT_NAME"))) {
				p.setProject_name(e.get("PROJECT_NAME").toString());
			}
			if (!M_Util.isEmpty(e.get("ORACLE_NUMBER"))) {
				p.setOracle_number(e.get("ORACLE_NUMBER").toString());
			}
			if (!M_Util.isEmpty(e.get("RELEASE_MONTH"))) {
			p.setRelease_month(e.get("RELEASE_MONTH").toString());
			}
			
			p.setiT_budget_amount(((BigDecimal) e.get("IT_BUDGET_AMOUNT"))
					.intValue());
			// p.set(e.get("APPROVE_FLAG").toString());
			// p.set(e.get("BUDGETED_FLAG").toString());
			// p.set(e.get("CANCEL_FLAG").toString());
			p.setStart_date((Date) e.get("START_MONTH"));
			p.setEnd_date((Date) e.get("END_MONTH"));

			if (!M_Util.isEmpty(e.get("VP_ID"))) {
				p.setVp(getPersonById(e.get("VP_ID").toString()));
			}
			if (!M_Util.isEmpty(e.get("DIRECTOR_ID"))) {
				p.setDirector(getPersonById(e.get("DIRECTOR_ID").toString()));
			}
			if (!M_Util.isEmpty(e.get("PM_NAME"))) {
				p.setPm_name(e.get("PM_NAME").toString());
			}
			if (!M_Util.isEmpty(e.get("BUSINESS_OWNER"))) {
				p.setBussiness_owner(e.get("BUSINESS_OWNER").toString());
			}

			// p.set(e.get("ACTIVE_STATUS").toString());
			if (!M_Util.isEmpty(e.get("YEAR"))) {
			
				p.setBudget_year(e.get("YEAR").toString());
			}

			
			List caList = new ArrayList<CAProfile>();
			List cadSelectOneList = new ArrayList<SelectItem>();
			for(Map caDao: (List<Map>)projectSearchDao.getCAProfilesByProjectId(p.getProject_id())  ){
			
				CAProfile cap = new CAProfile();
				cap.setCa_profile_id(caDao.get("CA_PROFILE_ID").toString());
				
				if (!M_Util.isEmpty(caDao.get("CA_NUMBER"))) {
				cap.setCa_number(caDao.get("CA_NUMBER").toString());
				}
				
				if (!M_Util.isEmpty(caDao.get("CA_NAME"))) {
				cap.setCa_name(caDao.get("CA_NAME").toString());
				}
				
				
				if (!M_Util.isEmpty(caDao.get("PROJECT_ID"))) {
				cap.setProject_id(caDao.get("PROJECT_ID").toString());
				}
				
				if (!M_Util.isEmpty(caDao.get("START_YEAR"))) {
				cap.setStart_year(caDao.get("START_YEAR").toString());
				}
				
				if (!M_Util.isEmpty(caDao.get("CA_STATUS_ID"))) {
				cap.setCa_status_id(caDao.get("CA_STATUS_ID").toString());
				}
				
				
				if (!M_Util.isEmpty(caDao.get("DESCRIPTION"))) {
				cap.setDescription(caDao.get("DESCRIPTION").toString());
				}
				
				
				cap.setCapex_total_labour_cost(((BigDecimal) caDao.get("CAPEX_TOTAL_LABOUR_COST")).intValue());
				
				cap.setCapex_release_cost(((BigDecimal)caDao.get("CAPEX_RELEASE_COST")).intValue());
				cap.setCapex_hardware_cost(((BigDecimal)caDao.get("CAPEX_HARDWARE_COST")).intValue());
				cap.setCapex_software_cost(((BigDecimal)caDao.get("CAPEX_SOFTWARE_COST")).intValue());
				cap.setCapex_other_cost(((BigDecimal)caDao.get("CAPEX_OTHER_COST")).intValue());
				cap.setOpex_software_cost(((BigDecimal)caDao.get("OPEX_SOFTWARE_COST")).intValue());
				cap.setOpex_hardware_cost(((BigDecimal)caDao.get("OPEX_HARDWARE_COST")).intValue());
				cap.setOpex_labour_cost(((BigDecimal)caDao.get("OPEX_LABOUR_COST")).intValue());
				
				cap.setOpex_tundra_cost(((BigDecimal)caDao.get("OPEX_TUNDRA_COST")).intValue());
				
				
				cap.setOpex_other_cost(((BigDecimal)caDao.get("OPEX_OTHER_COST")).intValue());
				
				
				if (!M_Util.isEmpty(caDao.get("CA_NAME"))) {
				cap.setCa_name(caDao.get("CA_NAME").toString());
				}
				
				if (!M_Util.isEmpty(caDao.get("YEAR_ID"))) {
				//	String year= getYearById(e.get("YEAR").toString());
				cap.setYear(caDao.get("YEAR_ID").toString());
				}
				
				if (!M_Util.isEmpty(caDao.get("ACTIVE_STATUS"))) {
				cap.setActive_status(((String)caDao.get("ACTIVE_STATUS")).charAt(0));
				}
				
				if (!M_Util.isEmpty(caDao.get("SCOA_BENEFITING"))) {
				cap.setScoa_benefiting(caDao.get("SCOA_BENEFITING").toString());
				}
				
				if (!M_Util.isEmpty(caDao.get("OPEX_COST_SAVING"))) {
				cap.setOpex_cost_saving(caDao.get("OPEX_COST_SAVING").toString());
				}
				
				caList.add(cap);
				
				cadSelectOneList.add(new SelectItem(cap.getCa_profile_id(), cap.getCa_number()));
			}
			 
			p.setCaProfileList(caList);
			p.setCADropDownList(cadSelectOneList);
			vps.add(p);
		}
		return vps;
	}

	public List<Reporting_person> getListOfVPs() {
		List<Reporting_person> vps = new ArrayList<Reporting_person>();
		
		for (Object e : projectSearchDao.getListOfVPs()) {
			Reporting_person p = new Reporting_person();
			p.setId(((Map) e).get("RG_ID").toString());
			p.setFirst_name((String) ((Map) e).get("RG_FIRST_NAME"));
			p.setLast_name((String) ((Map) e).get("RG_LAST_NAME"));
			// p.setId((String) ((Map)e).get("rg_id"));
			vps.add(p);
		}
		return vps;
	}

	// RG_ID,RG_FIRST_NAME,RG_NOTES,RG_LAST_NAME,POSITION
	public Reporting_person getPersonById(String id) {
		Reporting_person p = new Reporting_person();
		Map m = projectSearchDao.getPersonById(id);
		p.setId(m.get("RG_ID").toString());
		p.setFirst_name((String) m.get("RG_FIRST_NAME"));
		p.setLast_name((String) m.get("RG_LAST_NAME"));
		p.setNotes((String) m.get("RG_NOTES"));
		p.setPosition((String) m.get("POSITION"));
		return p;

	}

	public CAProfile getCAProfileFromListById(String cAProfileId,List<CAProfile> caList) {
		CAProfile selectedCA = null;
		for ( CAProfile ca: caList){
			
			if(ca.getCa_profile_id().equals(cAProfileId)){
				selectedCA = ca;
			}
		}
		
		
		
		return selectedCA;
	}

	public String getYearById(String id) {
		String year=(String) ((Map)projectSearchDao.getYearById(id)).get("YEAR");
		return year;
	}

}
